var app = getApp(),
  $ = require("../../utils/httpHelper.js")
var rd_session = wx.getStorageSync('rd_session');
Page({
  data: {
    hasAddress: false,
    addr: '',
    good_info: '',
    good_num: '',
    sell_price: 0,
    total_fee: 0,
    real_fee: 0,
    good_id: '',
    addr_id:0,
    liuyan: '',
    pay_cash: false,
    carlist: [],
    fromcar: false,
    shopname: '',
    coupon_list: [],
    fee: 0,
    coupon_id: '',
    sku_id: '',
    express_fee:''
  },

  onLoad: function (options) {
    var that = this;
    console.log(options)
    if (!app.check_empty(options.from) && options.from == 'car') {
      $.httpPost("cart_list", { rd_session: rd_session, coupon_id: this.data.coupon_id, status: 1 }, (data, err) => {
        this.setData({
          carlist: data.data.list,
          total_fee: data.data.total_fee,
          real_fee: data.data.real_fee,
          good_num: data.data.ctotal_num,
          express_fee: data.data.express_fee,
          fromcar: true,
        })
      })
    } else {
      let par = {
        good_id: options.good_id,
        num: options.num,
        rd_session: rd_session
      }
      $.httpPost("pay_info", par, (data, err) => {
        this.setData({
          good_info: data.data,
          good_num: options.num,
          good_id: options.good_id,
          sell_price: data.data.sell_price,
          total_fee: data.data.total_fee,
          real_fee: data.data.real_fee
        })
      })
    }
  },
  onShow(){
    this.getAddr()
  },
  getAddr(){
    $.httpPost("get_default_address  ", { rd_session: rd_session }, (data, err) => {
      if(data.result == 'error'){
        this.setData({
          hasAddress:false
        })
      }else{
        this.setData({
          hasAddress: true,
          addr:data.data.data,
          addr_id:data.data.data.id
        })
      }
    })
  },
  tonewAddress(){
    wx.navigateTo({
      url: '/pages/address/address?from=obl'
    })
  },
  tochooseaddr(){
    wx.navigateTo({
      url: '/pages/address/addressli?from=obl'
    })
  },
  message(e) {
    this.setData({
      liuyan: e.detail.value
    })
  },
  topay() {
    if (this.data.pay_cash) {
      this.submit()
    } else {
      //余额
      if (app.check_empty(this.data.password)) {
        wx.showToast({
          title: '请输入密码',
          duration: 2000
        })
      } else {
        this.submit()
      }
    }
  },

  subcheck() {
    console.log('che')
    $.httpPost("member_info", { rd_session: app.globalData.userInfo.rd_session }, (data, err) => {
      if (data.data.data.name == null) {
        wx.showModal({
          title: '提示',
          content: '请先在【我的】->【会员信息】完善信息~',
        })
      } else {
        this.submit()
      }
    })
  },

  submit() {
    if (this.data.addr_id == 0){
      wx.showToast({
        title: '请选择收货地址！',
      })
    }else{
      if (this.data.fromcar) {
        //来自购物车的订单
        let params = {
          message: this.data.liuyan,
          rd_session: app.globalData.userInfo.rd_session,
          address_id: this.data.addr_id
        }
        $.httpPost("submit_cart_order", params, (data, err) => {
          //去支付
          if (data.result == 'success') {
            app.wxPayGood({
              order_no: data.order_no, openid: app.globalData.userInfo.openId,
              success: function () {
                wx.showToast({
                  title: '支付成功哈',
                })
                wx.switchTab({
                  url: '/pages/mine/mine',
                })
              },
              fail: function () {
                wx.showToast({
                  title: '支付失败了呢',
                })
              }
            })
          } else {
            wx.showToast({
              title: data.message,
            })
          }
        })
      } else {
        let params = {
          message: this.data.liuyan,
          rd_session: app.globalData.userInfo.rd_session,
          total_num: this.data.good_num,
          good_id: this.data.good_id,
          address_id: this.data.addr_id
        }
        $.httpPost("submit_order", params, (data, err) => {
          if (data.result == 'success') {
            app.wxPayGood({
              order_no: data.order_no, openid: app.globalData.userInfo.openId,
              success: function () {
                wx.showToast({
                  title: '支付成功哈',
                })
                wx.switchTab({
                  url: '/pages/mine/mine',
                })
              },
              fail: function () {
                wx.showToast({
                  title: '支付失败了呢',
                })
              }
            })
          }
        })
      }
    }
    

  }

})  